﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TARS.User;

namespace TARS.AdminCP
{
    public partial class NewEmployee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                loaddata();
        }
        protected void loaddata()
        {
            using (var db = new TARSDelivery_EprojectEntities())
            {
                var load = db.t_BranchesInfo;
                cbbBraches.DataSource = load.ToList();
                cbbBraches.DataTextField = "Branches_Name";
                cbbBraches.DataValueField = "ID";
                cbbBraches.DataBind();
            }
        }
        protected void SendMail()
        {
            // Gmail Address from where you send the mail
            var fromAddress = "khanhquang14@gmail.com";
            // any address where the email will be sending
            var toAddress = txtEmail.Text.ToString();
            //Password of your gmail address
            const string fromPassword = "diemhong90";
            // Passing the values and make a email formate to display
            string subject = "User Passord";
            string body = "From: " + "khanhquang14@gmail.com" + "\n";
            body += "To: " + txtEmail.Text + "\n";
            body += "Username: " + txtUserName.Text.Trim();
            body += "\nPassword: " + txtPassword.Text.Trim();
            // smtp settings
            var smtp = new System.Net.Mail.SmtpClient();
            {
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
                smtp.Timeout = 20000;
            }
            // Passing values to smtp object
            smtp.Send(fromAddress, toAddress, subject, body);
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {

        }

        protected void btnRegist_Click1(object sender, EventArgs e)
        {
        }

        protected void btnAutoCreate_Click(object sender, EventArgs e)
        {
            
            if (Page.IsValid)
            {
                if (btnAutoCreate.Text != "Submit")
                {
                    txtPassword.Text = txtLName.Text.Replace(" ", "") + txtSSN.Text.Remove(0, txtSSN.Text.Length - 4);
                    txtUserName.Text = txtFName.Text.Replace(" ", "") + DateTime.Now.Day.ToString();
                    btnAutoCreate.Text = "Submit";
                }
                else
                {
                    using (var db = new TARSDelivery_EprojectEntities())
                    {
                        SendMail();
                        DateTime dob = Convert.ToDateTime(txtDOB.SelectedDate.ToString());
                        Guid braches = new Guid(cbbBraches.SelectedValue.ToString());
                        DateTime time = Convert.ToDateTime(DateTime.Now.ToString());

                        string type = cbbUsertype.SelectedValue.ToString();
                        var inserNewRegist = db.Insert_User(txtUserName.Text.Trim(), Encryption.GetMd5Hash(txtPassword.Text.Trim()), braches, time, type);
                        var load = db.t_User.Where(x => x.Username == txtUserName.Text.Trim());
                        foreach (var ss in load)
                        {
                            var insert = db.Insert_EmployeeInfo(txtFName.Text, txtFName.Text, txtAddress.Text, txtCity.Text,
                                txtState.Text, txtZipCode.Text, txtSSN.Text, dob, ss.ID, time,txtEmail.Text,txtPhone.Text);
                        }
                        lblConguralation.Visible = true;
                    }
                }
            }
        }
    }
}